package com.example.videolib

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.SurfaceView
import android.widget.LinearLayout
import android.view.SurfaceHolder
import tv.danmaku.ijk.media.player.IjkMediaPlayer
import java.io.IOException
import android.view.Gravity
import android.text.TextUtils
import android.util.Log


class YWVideoPlayView : LinearLayout, YWVideoPlayListener {
    private var mMediaPlayer: IjkMediaPlayer? = null
    var mPath = ""
    lateinit var surfaceView: SurfaceView
    var listener1: VideoPlayerListener? = null
        set(value) {
            field = value
            if (mMediaPlayer != null) {
                mMediaPlayer!!.setOnPreparedListener(value)
            }
        }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView()
    }

    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.view_layout_video, this, true)
        surfaceView = findViewById(R.id.play_video_sv)

    }

    /**
     * 设置视频地址。
     * 根据是否第一次播放视频，做不同的操作。
     *
     * @param path the path of the video.
     */
    fun setVideoPath(path: String) {
        if (TextUtils.equals("", mPath)) {
            //如果是第一次播放视频，那就创建一个新的surfaceView
            mPath = path
            createSurfaceView()
        } else {
            //否则就直接load
            mPath = path
            load()
        }
    }

    /**
     * 新建一个surfaceview
     */
    private fun createSurfaceView() {
        //生成一个新的surface view
        surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                Log.d("IJKMEDIA====", "surfaceCreated: ")
            }

            override fun surfaceChanged(
                holder: SurfaceHolder,
                format: Int,
                width: Int,
                height: Int
            ) {
                Log.d("IJKMEDIA====", "surfaceChanged: ")
                //surfaceview创建成功后，加载视频
                load()
            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {
                Log.d("IJKMEDIA====", "surfaceDestroyed: ")
            }

        })
    }

    /**
     * 加载视频
     */
    private fun load() {
        //每次都要重新创建IMediaPlayer
        createPlayer()
        try {
            mMediaPlayer!!.dataSource = mPath
        } catch (e: IOException) {
            e.printStackTrace()
        }
        //给mediaPlayer设置视图
        mMediaPlayer!!.setDisplay(surfaceView.holder)
        mMediaPlayer!!.prepareAsync()
    }


    /**
     * 创建一个新的player
     */
    private fun createPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.stop()
            mMediaPlayer!!.setDisplay(null)
            mMediaPlayer!!.release()
        }
        val ijkMediaPlayer = IjkMediaPlayer()
        IjkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG)

//开启硬解码        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
        mMediaPlayer = ijkMediaPlayer
        if (listener1 != null) {
            mMediaPlayer!!.setOnPreparedListener(listener1)
            mMediaPlayer!!.setOnInfoListener(listener1)
            mMediaPlayer!!.setOnSeekCompleteListener(listener1)
            mMediaPlayer!!.setOnBufferingUpdateListener(listener1)
            mMediaPlayer!!.setOnErrorListener(listener1)
        }
    }

    /**
     * -------======--------- 下面封装了一下控制视频的方法
     */
    override fun start() {
        Log.d("IJKMEDIA====", "start: ")
        if (mMediaPlayer != null) {
            mMediaPlayer!!.start()
        }
    }

    fun release() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.reset()
            mMediaPlayer!!.release()
            mMediaPlayer = null
        }
    }

    fun pause() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.pause()
        }
    }

    override fun stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.stop()
        }
    }


    fun reset() {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.reset()
        }
    }


    fun getDuration(): Long {
        return if (mMediaPlayer != null) {
            mMediaPlayer!!.duration
        } else {
            0
        }
    }


    fun getCurrentPosition(): Long {
        return if (mMediaPlayer != null) {
            mMediaPlayer!!.currentPosition
        } else {
            0
        }
    }


    fun seekTo(l: Long) {
        if (mMediaPlayer != null) {
            mMediaPlayer!!.seekTo(l)
        }
    }

//
//    override fun start() {
//        surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
//            override fun surfaceCreated(holder: SurfaceHolder) {
//
//            }
//
//            override fun surfaceChanged(
//                holder: SurfaceHolder,
//                format: Int,
//                width: Int,
//                height: Int
//            ) {
//                val ijkMediaPlayer = IjkMediaPlayer()
//                IjkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG)
//                ijkMediaPlayer.dataSource = mPath
//                ijkMediaPlayer.setDisplay(surfaceView.holder)
//                ijkMediaPlayer.prepareAsync()
//            }
//
//            override fun surfaceDestroyed(holder: SurfaceHolder) {
//            }
//
//        })
//
//    }
//
//    override fun stop() {
//        TODO("Not yet implemented")
//    }

    override fun moveOut() {
        TODO("Not yet implemented")
    }

    override fun moveIn() {
        TODO("Not yet implemented")
    }

    override fun moveUp() {
        TODO("Not yet implemented")
    }

    override fun moveDown() {
        TODO("Not yet implemented")
    }

    override fun bigger() {
        TODO("Not yet implemented")
    }

    override fun smaller() {
        TODO("Not yet implemented")
    }
}